var chechAll=$('#check-all');
var selectInvert=$('#select-invert');
var cutOut=$('#cut-out');
var classNewdata=[];
var paging=$('#nav-num');
var addphoto;
var revisephoto;
//*****************************获取数据*****************************//
var classData=JSON.parse(localStorage.getItem('aClass'))||[];
var index=JSON.parse(localStorage.getItem('index')) || 0;
//-----------------------插入数据--------------------------//
function render(){
    $('#content-tbody').html('');
    $.each(classNewdata,function (i) {
        $('<tr class="table-tr" data-index="'+classNewdata[i].index+'"><td class="content-th list-th "><input type="checkbox" class="checkbox"></td><td class="content-th class-data" id="num">'+classNewdata[i].orderNumber+'</td><td class="content-th class-data">'+classNewdata[i].class+'</td><td class="content-th width-th class-data">'+classNewdata[i].aTeacher+'</td><td class="content-th width-th class-data">'+classNewdata[i].bTeacher+'</td><td class="content-th width-th class-data">'+classNewdata[i].cTeacher+'</td><td class="content-th width-th class-data">'+classNewdata[i].LifeTeacher+'</td><td class="content-th width-th class-data">'+classNewdata[i].ClassSize+'</td><td class="content-th width-th class-data">'+classNewdata[i].Classroom+'</td><td class="content-th class-data">'+classNewdata[i].SchoolHours+'</td><td class="content-th last-th" ><span class="content-button content-correct" ><i class="fa fa-pencil-square-o" aria-hidden="true"></i></span><span class="content-button content-del"><i class="fa fa-trash" aria-hidden="true"></i></span></td><td class="content-th time-th">'+classNewdata[i].lassDepict+'</td></tr>').appendTo($('#content-tbody'))
    });
    localStorage.setItem('aClass',JSON.stringify(classData));
    localStorage.setItem('index',index);
}
//*****************************获取页码*****************************//
function getPageNum(){
    var num=Math.ceil(classData.length/6);
    $('#nav-num').html('');
    for (var i=0;i<num;i++) {
        var aLi=$('<li><span>'+(i+1)+'</span></li>');
        paging.append(aLi);
        if(i==0){
            $("li").addClass('active');
        }
    }
}
getPageNum();
function getPageDate(index){
    classNewdata=classData.slice((index-1)*6,6*index);
    render()
}
getPageDate(1);
$('#nav-num').on('click','span',function () {
    getPageDate($(this).html());
    $(this).parent('li').addClass('active').siblings('li').removeClass('active');
});
//*****************************图片上传*****************************//
$('.revise-photo').mouseover(function () {
    $(this).css('color','#3ab5f1')
});
$('.revise-photo').mouseout(function () {
    $(this).css('color','#333')
});
$('.hold-photo').mouseover(function () {
    $(this).css('color','#3ab5f1')
});
$('.hold-photo').mouseout(function () {
    $(this).css('color','#333')
});
$('.revise-photo').click(function () {
    $('.photo-file').click();
});
//*****************************图片修改*****************************//
$('.re-photo').mouseover(function () {
    $(this).css('color','#3ab5f1')
});
$('.re-photo').mouseout(function () {
    $(this).css('color','#333')
});
$('.re-hold-photo').mouseover(function () {
    $(this).css('color','#3ab5f1')
});
$('.re-hold-photo').mouseout(function () {
    $(this).css('color','#333')
});
$('.re-photo').click(function () {
    $('.re-file').click();
});
//*****************************全选/反选/删除*****************************//
chechAll.click(function (){
    var check=false;
    for (var i=0;i<$('.checkbox').length;i++){
        if ($('.checkbox').eq(i).is(':checked')==false){
            check=true;
        }
    }
    if (check){
        $('.checkbox').prop('checked',true);
    }else{
        $('.checkbox').prop('checked',false);
    }
});
selectInvert.click(function () {
    for (var i=0;i<$('.checkbox').length;i++){
        if ($('.checkbox').eq(i).is(':checked')){
            $('.checkbox').eq(i).prop("checked",false);
        }
        else {
            $('.checkbox').eq(i).prop("checked",true);
        }
    }
});
$('#cut-out').click(function () {
    var alenght=$('input:checked').length;
    if (alenght==0){
       $('#modal-text').text('请勾选班级信息！');
       $('#modal-keep').text('确认');
   }else {
        $('#modal-text').text('确认删除吗？');
        $('#modal-keep').text('是的确认删除！');
    }
});
$('#modal-keep').click(function () {
    $('.checkbox').each(function () {
        if ($(this).is(':checked')==true){
            var n=$(this).parents('tr').attr('data-index');
            for(var i=0;i<classData.length;i++){
                if(classData[i].index==n){
                    classData.splice(i,1);
                }
            }
            getPageNum();
            getPageDate(1);
        }
    });
});
//*****************************验证*****************************//
function  testing(n,m){
    if (n.search(m)==-1) {
        return false
    }
    else {
        return true
    }
}
//*****************************添加*****************************//
$('#content-hold').modal('hide');
$('#addClass').click(function () {
    $('#class-name').val('');
    $('#class').val('');
    $('#aTeacher').val('');
    $('#bTeacher').val('');
    $('#cTeacher').val('');
    $('#lifeTeacher').val('');
    $('#class-number').val('');
    $('#class-room').val('');
    $('#class-time').val('');
    $('#class-depict').val('');
    $('.photo').attr('src');
});
var revisePhoto=document.querySelector('.photo-file');
var holdPhoto=document.querySelector('.hold-photo');
holdPhoto.onclick=function () {
    var rpReader=new FileReader();
    rpReader.readAsDataURL(revisePhoto.files[0]);
    rpReader.onloadend=function () {
        addphoto=event.target.result;
        $('.photo').attr('src',addphoto);
    };
};
$('#content-hold').click(function () {
    var equalOrder=true;
    var classNam=$('#class-name').val();
    var aclass=$('#class').val();
    var aTeacher=$('#aTeacher').val();
    var bTeacher=$('#bTeacher').val();
    var cTeacher=$('#cTeacher').val();
    var lifeTeacher=$('#lifeTeacher').val();
    var classNumber=$('#class-number').val();
    var classRoom=$('#class-room').val();
    var classTime=$('#class-time').val();
    var cclassDepict=$('#class-depict').val();
    var photo=$('.photo').attr('src');
    var classOrder=testing(classNam,/^\d{4}$/);
    var classNews=testing(aclass,/^[\u4e00-\u9fa5]{2}$/);
    var classaTeacher=testing(aTeacher,/^[\u4e00-\u9fa5]{2,5}$/);
    var classbTeacher=testing(bTeacher,/^[\u4e00-\u9fa5]{2,5}$/);
    var classcTeacher=testing(cTeacher,/^[\u4e00-\u9fa5]{2,5}$/);
    var classlifeTeacher=testing(lifeTeacher,/^[\u4e00-\u9fa5]{2,5}$/);
    var classPeople=testing(classNumber,/^\d{2}$/);
    var schoolRoom=testing(classRoom,/^\d{3}$/);
    var classBegin=testing(classTime,/^[0-9][0-9][0-9][0-9]+(.[0-9]{1,2})$/);
    for (var i=0;i<classData.length;i++){
        if (classData[i].orderNumber==classNam){
            equalOrder=false;
        }
    }
    // console.log( equalOrder);
    if (classOrder&&classNews&&classaTeacher&&classbTeacher&&classcTeacher&&classlifeTeacher&&classPeople&&schoolRoom&&classBegin&&equalOrder){
        $('#myModal').modal('hide');
        $('.content-text').css('border','1.5px solid #cccccc');
        console.log(equalOrder);
        index++;
        classData.push({
            'orderNumber':classNam,
            'class':aclass,
            'aTeacher':aTeacher,
            'bTeacher':bTeacher,
            'cTeacher':cTeacher,
            'LifeTeacher':lifeTeacher,
            'ClassSize':classNumber,
            'Classroom':classRoom,
            'SchoolHours':classTime,
            'lassDepict':cclassDepict,
            'photo':photo,
            'index':index,
        },);
        getPageNum();
        getPageDate(1);
    }else {
        $('#myModal').modal('show');
        $('.content-text').css('border','1.5px solid red');
    }
});
//*****************************修改*****************************//
var m;
$('#content-tbody').on('click','.content-correct',function () {
    m=$(this).parents('tr').attr('data-index');
    for (var i=0;i<classData.length;i++) {
        if(classData[i].index==m){
            $('#className').val(classData[i].orderNumber);
            $('#aclass').val(classData[i].class);
            $('#a-teacher').val(classData[i].aTeacher);
            $('#b-teacher').val(classData[i].bTeacher);
            $('#c-teacher').val(classData[i].cTeacher);
            $('#life-teacher').val(classData[i].LifeTeacher);
            $('#classNumber').val(classData[i].ClassSize);
            $('#classRoom').val(classData[i].Classroom);
            $('#classTime').val(classData[i].SchoolHours);
            $('#classDepict').val(classData[i].lassDepict);
            $('#revise-photo').attr('src',classData[i].photo);
            $('#className').attr('disabled','disabled');
        }
    }
    $('#revise-modal').slideToggle(400);
    return m;
});
$('#content-tbody').on("mouseover mouseout",'.content-correct',function(event){
    if(event.type == "mouseover"){
        $(this).css('color','#f9a101');
    }else if(event.type == "mouseout"){
        $(this).css('color','#333');
    }
});
$('.aBtn,.cBtn').click(function () {
    $('#revise-modal').slideToggle(400);
});
var revisePh=document.querySelector('#file-photo');
var holdPh=document.querySelector('#hold-photo');
holdPh.onclick=function () {
    var rpReader=new FileReader();
    rpReader.readAsDataURL(revisePh.files[0]);
    rpReader.onloadend=function () {
        revisephoto=event.target.result;
        $('#revise-photo').attr('src',revisephoto);
    };
};
$('.bBtn').click(function () {
    var classNam=$('#className').val();
    var aclass=$('#aclass').val();
    var aTeacher=$('#a-teacher').val();
    var bTeacher=$('#b-teacher').val();
    var cTeacher=$('#c-teacher').val();
    var lifeTeacher=$('#life-teacher').val();
    var classNumber=$('#classNumber').val();
    var classRoom=$('#classRoom').val();
    var classTime=$('#classTime').val();
    var cclassDepict=$('#classDepict').val();
    var photo=$('#revise-photo').attr('src');
    var classNews=testing(aclass,/^[\u4e00-\u9fa5]{2}$/);
    var classaTeacher=testing(aTeacher,/^[\u4e00-\u9fa5]{2,5}$/);
    var classbTeacher=testing(bTeacher,/^[\u4e00-\u9fa5]{2,5}$/);
    var classcTeacher=testing(cTeacher,/^[\u4e00-\u9fa5]{2,5}$/);
    var classlifeTeacher=testing(lifeTeacher,/^[\u4e00-\u9fa5]{2,5}$/);
    var classPeople=testing(classNumber,/^\d{2}$/);
    var schoolRoom=testing(classRoom,/^\d{3}$/);
    var classBegin=testing(classTime,/^[0-9][0-9][0-9][0-9]+(.[0-9]{1,2})$/);
    if (classNews&&classaTeacher&&classbTeacher&&classcTeacher&&classlifeTeacher&&classPeople&&schoolRoom&&classBegin){
        $('.contentText').css('border','1.5px solid #cccccc');
        for(var i=0;i<classData.length;i++){
            if(classData[i].index==m){
                classData.splice(i,1,{
                    'orderNumber':classNam,
                    'class':aclass,
                    'aTeacher':aTeacher,
                    'bTeacher':bTeacher,
                    'cTeacher':cTeacher,
                    'LifeTeacher':lifeTeacher,
                    'ClassSize':classNumber,
                    'Classroom':classRoom,
                    'SchoolHours':classTime,
                    'lassDepict':cclassDepict,
                    'photo':photo,
                    'index':m,
                },);
                getPageNum();
                getPageDate(1);
            }
        }
        $('#revise-modal').slideToggle(400);
    }else {
        alert(2);
        $('.contentText').css('border','1.5px solid red');
    }
});
//*****************************单独删除*****************************//
$('#content-tbody').on("mouseover mouseout",'.content-del',function(event){
    if(event.type == "mouseover"){
        $(this).css('color','#f90000');
    }else if(event.type == "mouseout"){
        $(this).css('color','#333');
    }
});
var nu;
$('#content-tbody').on('click','.content-del',function () {
    $('#Modal-q').modal('show');
    return nu=$(this).parents('tr').attr('data-index');
});
$('#modal-keep').click(function () {
    for(var i=0;i<classData.length;i++){
        if(classData[i].index==nu){
            classData.splice(i,1);
        }
    }
    getPageNum();
    getPageDate(1);
});
//*****************************搜索*****************************//
$('.fa-search').mouseout(function () {
    $('.fa-search').css('color','#3ab5f1');
});
$('.fa-search').mouseover(function () {
    $('.fa-search').css('color','#f9a101');
});
$('#searchFor').click(function () {
    var amodal=false;
    var searchFor=$('.content-input').val();
    for (var i=0;i<classData.length;i++){
        if (searchFor==classData[i].orderNumber) {
            $('#content-tbody').html('');
            $('<tr class="table-tr" data-index="'+classData[i].index+'"><td class="content-th list-th "><input type="checkbox" class="checkbox"></td><td class="content-th class-data" id="num">'+classData[i].orderNumber+'</td><td class="content-th class-data">'+classData[i].class+'</td><td class="content-th width-th class-data">'+classData[i].aTeacher+'</td><td class="content-th width-th class-data">'+classData[i].bTeacher+'</td><td class="content-th width-th class-data">'+classData[i].cTeacher+'</td><td class="content-th width-th class-data">'+classData[i].LifeTeacher+'</td><td class="content-th width-th class-data">'+classData[i].ClassSize+'</td><td class="content-th width-th class-data">'+classData[i].Classroom+'</td><td class="content-th class-data">'+classData[i].SchoolHours+'</td><td class="content-th last-th" ><span class="content-button content-correct" ><i class="fa fa-pencil-square-o" aria-hidden="true"></i></span><span class="content-button content-del"><i class="fa fa-trash" aria-hidden="true"></i></span></td><td class="content-th time-th">'+classData[i].lassDepict+'</td></tr>').appendTo($('#content-tbody'));
            $('#nav-num').html('');
            amodal=true;}
    }
    if (amodal==true){
        // $('.content-input').val('');
    } else {
        $('#modal-text').text('请输入正确序号！');
        $('#modal-keep').text('确认');
        $('#Modal-q').modal('show');
    }
});
$('.content-input').keyup(function () {
    var back=$('.content-input').val();
    if (event.keyCode==8){
        if (back==''){
            getPageNum();
            getPageDate(1);
        }
    }
});